<%=packageName ? "package ${packageName}\n\n" : ''%>

import org.springframework.dao.DataIntegrityViolationException
import org.codehaus.groovy.grails.commons.DefaultGrailsDomainClass

class ${className}Controller {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
		session.username='pnewnam'
        redirect(action: "list", params: params)
    }

    def list() {
		params.max = Math.min(params.max ? params.int('max') : 25, 100)
		[${propertyName}List: ${className}.list(params), ${propertyName}Total: ${className}.count()]
    }
	
    def create() {
        [${propertyName}: new ${className}(params)]
    }

    def save() {
        def ${propertyName} = new ${className}(params)
        if (!${propertyName}.save(flush: true)) {
            render(view: "create", model: [${propertyName}: ${propertyName}])
            return
        }

		flash.message = message(code: 'default.created.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), ${propertyName}.id])
        redirect(action: "show", id: ${propertyName}.id)
    }

    def show() {
        def ${propertyName} = ${className}.get(params.id)
        if (!${propertyName}) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), params.id])
            redirect(action: "list")
            return
        }

        [${propertyName}: ${propertyName}]
    }

    def edit() {
        def ${propertyName} = ${className}.get(params.id)
        if (!${propertyName}) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), params.id])
            redirect(action: "list")
            return
        }

        [${propertyName}: ${propertyName}]
    }

    def update() {
        def ${propertyName} = ${className}.get(params.id)
        if (!${propertyName}) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (${propertyName}.version > version) {<% def lowerCaseName = grails.util.GrailsNameUtils.getPropertyName(className) %>
                ${propertyName}.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: '${domainClass.propertyName}.label', default: '${className}')] as Object[],
                          "Another user has updated this ${className} while you were editing")
                render(view: "edit", model: [${propertyName}: ${propertyName}])
                return
            }
        }

        ${propertyName}.properties = params

        if (!${propertyName}.save(flush: true)) {
            render(view: "edit", model: [${propertyName}: ${propertyName}])
            return
        }

		flash.message = message(code: 'default.updated.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), ${propertyName}.id])
        redirect(action: "show", id: ${propertyName}.id)
    }

    def delete() {
        def ${propertyName} = ${className}.get(params.id)
        if (!${propertyName}) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), params.id])
            redirect(action: "list")
            return
        }

        try {
            ${propertyName}.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: '${domainClass.propertyName}.label', default: '${className}'), params.id])
            redirect(action: "show", id: params.id)
        }
    }

	def search() {
		
		def fooDomain = new DefaultGrailsDomainClass(${className}.class)
		println "${className}"
		println fooDomain.propertyName

		def criteria = ${className}.createCriteria()
		def results = criteria.list {
			//search on session subsystem filter
			if(fooDomain.propertyName == 'issue' ||
				fooDomain.propertyName == 'developmentTask' ||
				fooDomain.propertyName == 'deployment' ||
				fooDomain.propertyName == 'project') {
				if (session?.filterProject) {
					and {
						'in'('project',session?.filterProject)
					}
		
				}
			}

			fooDomain.properties.each{
				def value = params.get(it.name)
				


				if (params.containsKey(it.name) && value.toString().length() > 0) {
					def pName = it.name
					def pType = it.type.toString()
					def pTypeClazz = it.type
					println "pType-> " + pType
					println "pName-> " + pName
					println "pValue-> " + value
					

					if (pType.contains("String")) {
						and {
							ilike(pName, "%" + value +"%")
						}
					} else if (pType.contains("BigDecimal")) {
						and {
							eq(pName,new BigDecimal(params.int(pName)))
						}
					} else if (pType.contains("Integer")) {
						and {
							eq(pName,params.int(pName))
						}
					} else if (pType.contains("Long")) {
						and {
							eq(pName,params.long(pName))
						}
					} else if (pType.contains("Boolean")) {
						and {
							eq(pName,true)
						}
					} else {
					println "pValueAt-> " + value.getAt("id")
					println "->" + (value.getAt("id") != null)
						if (value.getAt("id") != null && value.getAt("id") != "null") {
							and {
								//def obj = new DefaultGrailsDomainClass(pTypeClazz)
								def obj = pTypeClazz.newInstance()
								def searchObj = obj.get(value.getAt("id"))
								//eq(pName, value.id)
								like(pName, searchObj)
							}
						}

					}
				}
			}
		}

		
		render(view:"list", model:[${propertyName}List: results, ${propertyName}Total: 0])



	}

}
